package com.jtech.together.view.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.jtech.together.R;
import com.jtech.together.model.UserModel;
import com.jtech.together.mvp.contract.LoginContract;
import com.jtech.together.mvp.presenter.LoginPresenter;
import com.jtech.together.util.ActivityJump;
import com.jtech.together.util.MD5;
import com.jtech.together.view.activity.base.BaseActivity;
import com.jtech.together.view.weight.dialog.CustomProgressDialog;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 登录页面
 * Created by wuxubaiyang on 2017/5/10.
 */
public class LoginActivity extends BaseActivity implements LoginContract.View, Toolbar.OnMenuItemClickListener, TextView.OnEditorActionListener {
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.textinputlayout_login_username)
    TextInputLayout textInputLayoutUserName;
    @Bind(R.id.textinputlayout_login_password)
    TextInputLayout textInputLayoutPassword;

    private LoginContract.Presenter presenter;

    @Override
    protected void initVariables(Bundle bundle) {
        //绑定P类
        presenter = new LoginPresenter(getActivity(), this, bundle);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        //设置标题栏
        setSupportActionBar(toolbar);
        //设置标题栏菜单点击事件
        toolbar.setOnMenuItemClickListener(this);
        //设置输入监听
        textInputLayoutUserName.getEditText()
                .addTextChangedListener(new OnTextChangeListener(textInputLayoutUserName));
        textInputLayoutPassword.getEditText()
                .addTextChangedListener(new OnTextChangeListener(textInputLayoutPassword));
        //设置键盘动作监听
        textInputLayoutPassword.getEditText()
                .setOnEditorActionListener(this);
    }

    @Override
    protected void loadData() {
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        toolbar.inflateMenu(R.menu.menu_done);
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 提交信息
     */
    private boolean submit() {
        String username = textInputLayoutUserName.getEditText()
                .getText().toString();
        if (TextUtils.isEmpty(username)) {
            textInputLayoutUserName.setError("用户名不能为空");
            return false;
        }
        String password = textInputLayoutPassword.getEditText()
                .getText().toString();
        if (TextUtils.isEmpty(password)) {
            textInputLayoutPassword.setError("密码不能为空");
            return false;
        }
        //发起登录
        presenter.login(username, MD5.to(password));
        CustomProgressDialog.showProgressDialog(getActivity(), "正在登录");
        return true;
    }

    @Override
    public void loginSuccess(UserModel userModel) {
        CustomProgressDialog.dismissProgressDialog();
        ActivityJump.build(getActivity(), MainActivity.class)
                .jump();
        onBackPressed();
    }

    @Override
    public void loginFail(String error) {
        CustomProgressDialog.dismissProgressDialog();
        Snackbar.make(toolbar, error, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.done://完成
                return submit();
        }
        return false;
    }

    /**
     * 切换到注册
     */
    @OnClick(R.id.imagebutton_login_switch)
    public void onSwitch() {
        ActivityJump.build(getActivity(), RegisterActivity.class)
                .jump();
        onBackPressed();
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            return submit();
        }
        return false;
    }

    /**
     * 当输入变化的时候
     */
    private class OnTextChangeListener implements TextWatcher {
        private TextInputLayout textInputLayout;

        public OnTextChangeListener(TextInputLayout textInputLayout) {
            this.textInputLayout = textInputLayout;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //清空错误
            if (!TextUtils.isEmpty(textInputLayout.getError())) {
                textInputLayout.setError("");
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
        MobclickAgent.onPause(this);
    }
}